package com.xyd.Servlet;

import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Biz.Impl.ProductCarBizImpl;
import com.xyd.Biz.ProductCarBiz;
import com.xyd.Entity.Commodity;
import com.xyd.Entity.ProductCar;
import com.xyd.Entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: PutInCarServlet
 * @Description:将商品添加到购物车
 * @Author 余全盛
 * @Date: 2021/10/15 16:14
 */
@WebServlet("/html/PutInCarServlet")
public class PutInCarServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProductCarBiz productCarBiz = new ProductCarBizImpl();
        HttpSession session=req.getSession();
        int productId = Integer.parseInt(req.getParameter("productid") == null || "".equals(req.getParameter("productid")) ? "0" : req.getParameter("productid"));
        String productName = req.getParameter("productname");
        double price = Double.parseDouble(req.getParameter("price") == null || "".equals(req.getParameter("price")) ? "0.00" : req.getParameter("price"));
        User user=(User) session.getAttribute("user");
        ProductCar productCar=productCarBiz.selectProductCarByUserIDAndProductId(productId,user.getUserid());
        if (productCar != null) {
            productCarBiz.updateProductCarNum(productCar.getProductid(),user.getUserid());
        }else{
            productCarBiz.addProductToCar(productId, productName, price, user.getUserid());
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
